package com.mt.api.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.modules.domain.AppStock;
import com.mt.api.modules.domain.vo.AppSkuGroupColorVo;
import com.mt.api.modules.manager.ICargoManager;
import com.mt.api.modules.manager.IStockManager;
import com.mt.api.util.*;
import com.mt.api.modules.domain.AppCargo;
import com.mt.api.modules.domain.AppSku;
import com.mt.api.modules.domain.vo.AppSkuVo;
import com.mt.api.modules.manager.impl.SkuManagerImpl;
import com.mt.api.modules.mapper.AppSkuMapper;
import com.mt.api.modules.mapstruct.AppSkuMapStruct;
import com.mt.api.modules.service.AppSkuService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author plover
 * @description 服务实现
 * @date 2020-07-06
 **/
@Service
@RequiredArgsConstructor
public class AppSkuServiceImpl extends ServiceImpl<AppSkuMapper, AppSku> implements AppSkuService {

    private final SkuManagerImpl skuManager;
    private final ICargoManager cargoManager;
    private final AppSkuMapStruct skuConvert;
    private final IStockManager stockManager;

    @Override
    public WebResponse listByBrandId() {
        List<AppSku> skus = baseMapper.selectList(Wrappers.<AppSku>lambdaQuery()
                .orderByAsc(AppSku::getColor, AppSku::getSize));
        return WebResponseUtils.success(skus);
    }

    @Override
    public WebResponse getAllCargoNo(String cargoNo) {
        List<AppCargo> cargoList = cargoManager.getCargoByBrandId();
        return WebResponseUtils.successList(cargoList);
    }

    @Override
    public WebResponse getByCargoId(Integer cargoId) {
        List<AppSkuVo> skuVoList = Lists.newArrayList();

        AppCargo cargo = cargoManager.getById(cargoId);
        if (Objects.isNull(cargo)) {
            return WebResponseUtils.errorParam("款号不存在或者已下架");
        }
        List<AppSku> appSkuList = skuManager.getListByCargoId(cargoId);
        if (CollectionUtils.isEmpty(appSkuList)) {
            return WebResponseUtils.successList(skuVoList);
        }

        appSkuList = ColorSizeSortUtil.skuSort(appSkuList);
        // 获取所有的skuId
        List<Integer> skuIds = appSkuList.stream().map(AppSku::getId).collect(Collectors.toList());

        List<AppStock> appStockList = stockManager.getListBySkuIds(skuIds);

        skuVoList = appSkuList.stream().map(appSku -> {
            AppSkuVo appSkuVo = skuConvert.toVo(appSku);
            appStockList.stream().filter(appStock -> Objects.equals(appStock.getSkuId(), appSku.getId()))
                    .findFirst().ifPresent(appStock -> appSkuVo.setQty(appStock.getQty()));
            return appSkuVo;
        }).collect(Collectors.toList());
        return WebResponseUtils.successList(skuVoList);
    }

    @Override
    public WebResponse getByCargoNoGroupColor(String cargoNo) {
        List<AppSkuGroupColorVo> skuVoList = Lists.newArrayList();
        LambdaQueryWrapper<AppSku> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AppSku::getCargoNo, cargoNo);
        List<AppSku> appSkuList = skuManager.list(wrapper);
        if (CollectionUtils.isEmpty(appSkuList)) {
            return WebResponseUtils.successList(skuVoList);
        }
        appSkuList = ColorSizeSortUtil.skuSort(appSkuList);

        Map<String, List<AppSku>> colorSkuMap = appSkuList.stream().collect(Collectors.groupingBy(AppSku::getColor));

        skuVoList = colorSkuMap.entrySet().stream().map(map -> {
            String color = map.getKey();
            List<AppSku> appSkus = map.getValue();
            AppSkuGroupColorVo appSkuGroupColorVo = new AppSkuGroupColorVo();
            appSkuGroupColorVo.setColor(color);
            List<AppSkuGroupColorVo.SimpleSkuVo> simpleSkuVoList = appSkus.stream().map(appSku -> {
                AppSkuGroupColorVo.SimpleSkuVo simpleSkuVo = new AppSkuGroupColorVo.SimpleSkuVo();
                simpleSkuVo.setSkuId(appSku.getId());
                simpleSkuVo.setSize(appSku.getSize());
                return simpleSkuVo;
            }).collect(Collectors.toList());
            appSkuGroupColorVo.setSkuInfoList(simpleSkuVoList);
            return appSkuGroupColorVo;
        }).collect(Collectors.toList());
        return WebResponseUtils.successList(skuVoList);
    }


    private boolean isSkuExists(AppSku appSku) {
        LambdaQueryWrapper<AppSku> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppSku::getCargoNo, appSku.getCargoNo())
                .eq(AppSku::getColor, appSku.getColor())
                .eq(AppSku::getSize, appSku.getSize());
        AppSku sku = baseMapper.selectOne(queryWrapper);
        return Objects.isNull(sku) ? false : true;
    }
}